package com.zhkc.wisdom.common.mybatis.encrypt.utils;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;


/**
 * Aes加密工具类
 *
 */
@Slf4j
public class AesKeyUtils {
    /**
     * 加密算法是AES
     */
    private static final String ALGORITHM = "AES";
    private static final Integer TWO = 2;

    private static final String SECURE_ALGORITHM = "SHA1PRNG";

    private static final int KEY_SIZE = 128;

    private AesKeyUtils() {
    }

    /**
     * 加密
     */
    public static String encrypt(String content, String keyStr) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
            SecureRandom secureRandom = SecureRandom.getInstance(SECURE_ALGORITHM);
            secureRandom.setSeed(keyStr.getBytes("UTF-8"));
            kgen.init(KEY_SIZE, secureRandom);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM);
            // 创建密码器
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            byte[] byteContent = content.getBytes("UTF-8");
            // 初始化
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(byteContent);
            // 加密
            return bytes2Hex(result);
        } catch (Exception e) {
            log.error("AesKeyUtils.encrypt Error : ", e);
        }
        return null;
    }

    /**
     * 解密
     */
    public static String decrypt(String content, String keyStr) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
            SecureRandom secureRandom = SecureRandom.getInstance(SECURE_ALGORITHM);
            secureRandom.setSeed(keyStr.getBytes("UTF-8"));
            kgen.init(KEY_SIZE, secureRandom);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, ALGORITHM);
            // 创建密码器
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            // 初始化
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(hexStr2Byte(content));
            // 加密
            return new String(result, "UTF-8");
        } catch (Exception e) {
            log.error("AesKeyUtils.decrypt Error : " + e);
        }
        return null;
    }

    /**
     * 转为16进制
     */
    private static String bytes2Hex(byte[] bts) {
        StringBuilder des = new StringBuilder();
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = Integer.toHexString(bts[i] & 0xFF);
            if (tmp.length() == 1) {
                des.append("0");
            }
            des.append(tmp);
        }
        return des.toString();
    }

    /**
     * 将16进制转换为二进制
     */
    public static byte[] hexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) {
            return new byte[0];
        }
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / TWO; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    public static void main(String[] args) {

        String encryptPass = "xzxxx";

        /**
         * encrypt = 83b4facb3fc46511bebeda19c60e7d11
         */

        String encrypt = AesKeyUtils.encrypt("17326134597", encryptPass);
        String decrypt = AesKeyUtils.decrypt("83b4facb3fc46511bebeda19c60e7d11", encryptPass);
        System.out.println("encrypt = " + encrypt);
        System.out.println("decrypt = " + decrypt);
    }

}